package kercar.manager;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import kercar.debug.Debugger;

public class PhotoManager {
	
	private static int NB_PHOTO = 0;
	private static final String COMMAND_SAVE_PHOTO = "fswebcam" + "-r"
			+ "960x720" + "-d" + "/dev/video0" + "/home/pi/photos/webcam"
			+ NB_PHOTO + ".jpg";
	

	public void takePhoto() {

		List<String> command = new java.util.ArrayList<String>();
		command.add(COMMAND_SAVE_PHOTO);

		ProcessBuilder probuilder = new ProcessBuilder(command);

		try {
			Process process = probuilder.start();
			StringBuilder out1 = new StringBuilder();

			InputStream is = process.getInputStream();
			InputStreamReader isr = new InputStreamReader(is);
			BufferedReader br = new BufferedReader(isr);
			String line = null;
			line = br.readLine();

			while (line != null) {
				out1.append(line);
				line = br.readLine();
			}

			process.waitFor();

			NB_PHOTO++;

		} catch (IOException e) {
			Debugger.logException(e);
		} catch (InterruptedException e) {
			Debugger.logException(e);
		}

	}

	public void photoSent() {
		NB_PHOTO = 0;
	}

	public int getNbPhoto() {
		return NB_PHOTO;
	}
}
